X-Git-Url: https://git.r.bdr.sh/rbdr/super-polarity/blobdiff_plain/9899ae5d21c559b98386be48c5a80e63db10552d..95d7601b7742ed560a9d8e00269217f62fc7ce32:/Super%20Polarity/MainShip.cs diff --git a/Super Polarity/MainShip.cs b/Super Polarity/MainShip.cs index 51d1eb3..ac7a775 100644 --- a/Super Polarity/MainShip.cs +++ b/Super Polarity/MainShip.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; namespace SuperPolarity @@ -11,12 +12,21 @@ namespace SuperPolarity { public Texture2D PlayerTexture; public Vector2 Position; + public Vector2 Origin; public bool Active; public int Lives; public int Multiplier; public int Score; public float Angle; + // Physics Properties + Vector2 Velocity; + Vector2 Acceleration; + + float MaxVelocity; + float AccelerationRate; + ParticleEngine particleEngine; + public int Width { get { return PlayerTexture.Width; } @@ -27,7 +37,7 @@ namespace SuperPolarity get { return PlayerTexture.Height; } } - public void Initialize(Texture2D texture, Vector2 position) + public void Initialize(ContentManager Content, Texture2D texture, Vector2 position) { PlayerTexture = texture; Position = position; @@ -35,15 +45,142 @@ namespace SuperPolarity Multiplier = 1; Lives = 3; Score = 0; + + Origin = new Vector2(PlayerTexture.Width / 2, PlayerTexture.Height / 2); + Velocity = new Vector2(0, 0); + Acceleration = new Vector2(0, 0); + + MaxVelocity = 5; + AccelerationRate = 10; + + List texturesList = new List(); + texturesList.Add(Content.Load("Graphics\\circle")); + texturesList.Add(Content.Load("Graphics\\diamond")); + texturesList.Add(Content.Load("Graphics\\star")); + + particleEngine = new ParticleEngine(texturesList, Position); + + BindInput(); + } + + void BindInput() + { + InputController.Bind("moveX", HandleHorizontalMovement); + InputController.Bind("moveY", HandleVerticalMovement); + } + + public void HandleHorizontalMovement(float value) + { + Acceleration.X = value * AccelerationRate; + } + + public void HandleVerticalMovement(float value) + { + Acceleration.Y = value * AccelerationRate; + } + + public void AutoDeccelerate(GameTime gameTime) + { + if (Acceleration.X == 0 && Velocity.X > 0) { + if (AccelerationRate * gameTime.ElapsedGameTime.TotalSeconds > Velocity.X) + { + Velocity.X = 0; + Acceleration.X = 0; + } + else + { + Acceleration.X = -AccelerationRate; + } + } + + if (Acceleration.X == 0 && Velocity.X < 0) + { + if (-AccelerationRate * gameTime.ElapsedGameTime.TotalSeconds < Velocity.X) + { + Velocity.X = 0; + Acceleration.X = 0; + } + else + { + Acceleration.X = AccelerationRate; + } + } + + if (Acceleration.Y == 0 && Velocity.Y > 0) + { + if (AccelerationRate * gameTime.ElapsedGameTime.TotalSeconds > Velocity.Y) + { + Velocity.Y = 0; + Acceleration.Y = 0; + } + else + { + Acceleration.Y = -AccelerationRate; + } + } + + if (Acceleration.Y == 0 && Velocity.Y < 0) + { + if (-AccelerationRate * gameTime.ElapsedGameTime.TotalSeconds < Velocity.Y) + { + Velocity.Y = 0; + Acceleration.Y = 0; + } + else + { + Acceleration.Y = AccelerationRate; + } + } + } + + public void Update(GameTime gameTime) + { + Move(gameTime); + ChangeAngle(); + particleEngine.EmitterLocation = Position; + particleEngine.Update(); + } + + public void Move(GameTime gameTime) + { + AutoDeccelerate(gameTime); + + Velocity.X = Velocity.X + Acceleration.X * (float) gameTime.ElapsedGameTime.TotalSeconds; + Velocity.Y = Velocity.Y + Acceleration.Y * (float) gameTime.ElapsedGameTime.TotalSeconds; + + if (Velocity.X > MaxVelocity) + { + Velocity.X = MaxVelocity; + } + + if (Velocity.X < -MaxVelocity) + { + Velocity.X = -MaxVelocity; + } + + if (Velocity.Y > MaxVelocity) + { + Velocity.Y = MaxVelocity; + } + + if (Velocity.Y < -MaxVelocity) + { + Velocity.Y = -MaxVelocity; + } + + Position.X = Position.X + Velocity.X; + Position.Y = Position.Y + Velocity.Y; } - public void Update() + public void ChangeAngle() { + Angle = (float) Math.Atan2(Velocity.Y, Velocity.X); } public void Draw(SpriteBatch spriteBatch) { - spriteBatch.Draw(PlayerTexture, Position, null, Color.White, Angle, Vector2.Zero, 1f, SpriteEffects.None, 0f); + particleEngine.Draw(spriteBatch); + spriteBatch.Draw(PlayerTexture, Position, null, Color.White, Angle, Origin, 1f, SpriteEffects.None, 0f); } } }